/*
 * Copyright (C) 2007-2008  Hylke van der Schaaf
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, in version 3 of the License.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package hextd.message;

import hextd.message.MessageFactory.NetworkMessage;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class MessageLoadMap implements GameMessage {

	public NetworkMessage type = NetworkMessage.LoadMap;
	public static String RegExp = "L ([\\S]+) ([\\S]+) ([\\S]+)";
	//String map, String mapMd5, String version
	public static Pattern MessagePattern = Pattern.compile(MessageLoadMap.RegExp);
	//
	private String message;
	private String mapLocation;
	private String mapMd5;
	private String version;

	public static String formatMessage(String mapLocation, String mapMd5, String version) {
		String message;
		try {
			message = "L " + mapLocation + " " + mapMd5 + " " + URLEncoder.encode(version, "UTF-8");
			return message;
		} catch (UnsupportedEncodingException ex) {
			Logger.getLogger(MessageLoadMap.class.getName()).log(Level.SEVERE, null, ex);
		}
		return "";
	}

	public MessageLoadMap() {
	}

	private MessageLoadMap(String message) {
		this.message = message;
		Matcher matcher = MessageLoadMap.MessagePattern.matcher(message);
		if (matcher.matches()) {
			try {
				this.mapLocation = matcher.group(1);
				this.mapMd5 = matcher.group(2);
				this.version = URLDecoder.decode(matcher.group(3), "UTF-8");
			} catch (UnsupportedEncodingException ex) {
				Logger.getLogger(MessageLoadMap.class.getName()).log(Level.SEVERE, null, ex);
			} catch (NumberFormatException e) {
				System.err.println("MessageLoadMap: invalid id in: " + matcher.group(1));
			}
		}
	}

	@Override
	public int getTick() {
		return 0;
	}

	@Override
	public String getMessage() {
		return this.message;
	}

	@Override
	public Message initFromString(String message) {
		return new MessageLoadMap(message);
	}

	@Override
	public int getSenderId() {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public void setSenderId(int id) {
		throw new UnsupportedOperationException("Not supported yet.");
	}

	@Override
	public Pattern getPattern() {
		return MessageLoadMap.MessagePattern;
	}

	@Override
	public NetworkMessage getType() {
		return this.type;
	}

	public String getMapLocation() {
		return mapLocation;
	}

	public String getMapMd5() {
		return mapMd5;
	}

	public String getVersion() {
		return version;
	}
}
